package cn.lisens.sm.config;

import java.util.EnumSet;

import org.springframework.context.annotation.Configuration;
import org.springframework.statemachine.config.EnumStateMachineConfigurerAdapter;
import org.springframework.statemachine.config.builders.StateMachineStateConfigurer;
import org.springframework.statemachine.config.builders.StateMachineTransitionConfigurer;

import cn.lisens.sm.enums.Events;
import cn.lisens.sm.enums.States;

@Configuration
public class StateMachineConfig extends EnumStateMachineConfigurerAdapter<States, Events>  {
	 @Override
	    public void configure(StateMachineStateConfigurer<States, Events> states) throws Exception {
	        states
	            .withStates()
	                .initial(States.SI)  // 初始状态
	                .states(EnumSet.allOf(States.class))
	                .end(States.SF);     // 结束状态
	    }

	    @Override
	    public void configure(StateMachineTransitionConfigurer<States, Events> transitions) throws Exception {
	        transitions
	            .withExternal()
	                .source(States.SI).target(States.S1).event(Events.E1)
	                .and()
	            .withExternal()
	                .source(States.S1).target(States.S2).event(Events.E2)
	                .and()
	            .withExternal()
	                .source(States.S2).target(States.SF).event(Events.EF);
	    }
}
